<?php

/**
 * This is the model class for table "{{cmp_news}}".
 *
 * The followings are the available columns in table '{{cmp_news}}':
 * @property integer $zcn_id
 * @property integer $zm_id
 * @property integer $zpc_id
 * @property string $zcn_title
 * @property string $zcn_pic
 * @property integer $zcn_status
 * @property integer $zcn_top
 * @property string $zt_ids
 * @property integer $zcn_hist
 * @property integer $zcn_aut_type
 * @property string $zcn_author
 * @property integer $inputtime
 * @property integer $updatetime
 */
class CmpNews extends CActiveRecord
{
	//商家名称,用于查询
	public $company_name;
	
	//审核状态,用于查询
	public $check_type;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CmpNews the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cmp_news}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zcn_content,zcn_title', 'required'),
			array('zm_id, zpc_id, zcn_status, zcn_top, zcn_hist, zcn_aut_type, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zcn_title, zcn_author,check_type,company_name', 'length', 'max'=>100),
			array('zcn_pic, zt_ids', 'length', 'max'=>255),
			array('zcn_content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zcn_id, zm_id, zpc_id, zcn_title, zcn_pic, zcn_status, zcn_top, zt_ids, zcn_hist, zcn_aut_type, zcn_author, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'zpc'=>array(self::BELONGS_TO, 'CmpCat', 'zpc_id'),
				'company'=>array(self::BELONGS_TO, 'Company', 'zm_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zcn_id' => 'Zcn',
			'zm_id' => 'Zm',
			'zpc_id' => 'Zpc',
			'zcn_title' => 'Zcn Title',
			'zcn_content' => '内容',
			'zcn_pic' => 'Zcn Pic',
			'zcn_status' => 'Zcn Status',
			'zcn_top' => 'Zcn Top',
			'zt_ids' => 'Zt Ids',
			'zcn_hist' => 'Zcn Hist',
			'zcn_aut_type' => 'Zcn Aut Type',
			'zcn_author' => 'Zcn Author',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zcn_id',$this->zcn_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zpc_id',$this->zpc_id);
		$criteria->compare('zcn_title',$this->zcn_title,true);
		$criteria->compare('zcn_content',$this->zcn_content);
		$criteria->compare('zcn_pic',$this->zcn_pic,true);	
		$criteria->compare('zcn_top',$this->zcn_top);
		$criteria->compare('zt_ids',$this->zt_ids,true);
		$criteria->compare('zcn_hist',$this->zcn_hist);
		$criteria->compare('zcn_aut_type',$this->zcn_aut_type);
		$criteria->compare('zcn_author',$this->zcn_author,true);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);

		//审核状态
		if(!empty($this->check_type)){
			if($this->check_type==3){
				$criteria->compare('zcn_status',array('1','2'));
			}else{
				$criteria->compare('zcn_status',$this->check_type);
			}
		}else{
			$criteria->compare('zcn_status',$this->zcn_status);
		}
		
		//根据商家名称
		if(!empty($this->company_name)){
			$criteria->join .= ' LEFT JOIN {{company}} c ON c.zm_id=t.zm_id ';
			$criteria->addCondition("c.zc_name like '%".$this->company_name."%'");
		}
		
		$criteria->order = 'inputtime DESC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->zm_id = Company::model()->getCmpUserId();
			$this->inputtime = time();
			if($this->zcn_aut_type==1){
				$this->zcn_author = Company::model()->findByPk(Company::model()->getCmpUserId())->zc_name;
			}elseif($this->zcn_aut_type==2){
				$this->zcn_author = '';
			}
		}else{
			$this->updatetime = time();
		}
		return true;
	}
	
	public function getCheckStatus($type=null){
		if($type==null){
			return array(
					'1'=>'待审核',
					'2'=>'已审核',
					'3'=>'全部',
					);
		}
	}
}